<?php

//This is the array library of Tamarin, containing some useful array funtions

function arrayPrepend($array, $values) {
	if(!is_array($values)) {
		$values = array($values);
	}
	$array = array_merge($values, $array);
	return $array;
}

function arrayAppend($array, $values) {
	if(!is_array($values)) {
		$values = array($values);
	}
	$array = array_merge($array, $values);
	return $array;
}

function arrayInsert($array, $values, $positionKey) {
	if(!is_array($values)) {
		$values = array($values);
	}
	for($i=0;list($key, $value) = each($array);$i++) {
		if($key==$positionKey) {
			end($array);
		}
	}
	$beginning = array_slice($array, 0, $i-1);
	$end = array_slice($array, $i-1);
	$array = array_merge($beginning, $values, $end);
	return $array;
}

function arrayValuesSimilar($array) {
	reset($array);
	$first = current($array);
	foreach($array as $value) {
		if($first!=$value) {
			return false;
		}
	}
	return true;
}

function arrayEliminateSimilar($array) {
	foreach($array as $key => $value) {
		$tmpArray = $array;
		unset($tmpArray[$key]);
		if(in_array($value, $tmpArray)) {
			unset($array[$key]);
		}
	}
	return $array;
}

//Use array_count_values() instead
function arrayInstancesOf($array, $value) {
	$number = 0;
	foreach($array as $rowValue) {
		if($rowValue==$value) {
			$number++;
		}
	}
	return $number;
}

function arrayExtend(array $original, array $extension) {
	foreach($extension as $key => $value) {
		if(!isset($original[$key])) {
			$original[$key] = $value;
		}
	}
	return $original;
}

function arrayFilter($original, $indexes) {
	$new = array();
	foreach($indexes as $index) {
		if(isset($original[$index])) {
			$new[$index] = $original[$index];
		}
	}
	$original = $new;
	return $original;
}

?>
